<?php
/****************************************************
Wer Wird Millionär
TIT10NS Webengeneering Projekt

manageadmins - Seite um Adminrechte zu verwalten

Aufgeteilt in zwei Formulare:
-Das Erste ist zuständig für die Auswahl des Benutzers
-Das Zweite ist zuständig für die Zuweisung der Rechte

Autoren: Markus Schwarz
Copyright: 2012/2013 DHBW Mannheim
****************************************************/
include ("inc/startup.php");
include ("inc/header.php");
require ("inc/TAdminView.php");

if (isset($_POST['searchUsers'])) {

	// Wird nur ausgefuehrt, wenn ein Benutzername ausgesucht wurde
	if($_POST['searchWord'] != "")
	{
		$searchWord = $_POST['searchWord'];
		$countUsers = TModel::countUsersWithSearchWord($searchWord);
		
		switch ($countUsers) {
			case 0:
				$notification = new TAdminNotification ("error", "manageAdminFormNoUserFound");
				TAdminView::getManageAdminForm($notification);
				break;
			case 1:
				$user = TModel::getUsersWithSearchWord($searchWord);
				
				// foreach wird nur genutzt um id abzugreifen
				foreach ($user as $id => $username) {
					$tuser = TUser::getUserById($id);
				}
				if ($tuser->getNickname() != 'root') {
					if ($_SESSION['user']->getUserid() == $tuser->getUserid()) {
						// Ansprechen der View um das Formular abzurufen
						$notification = new TAdminNotification ("error", "manageAdminFormChangeOwnRole");
						TAdminView::getManageAdminForm($notification);
					}
					else {
						$notification = new TAdminNotification ("", "");
						TAdminView::getManageUserRightsForm($notification, $tuser);
					}
				}
				else {
					// Ansprechen der View um das Formular abzurufen
					$notification = new TAdminNotification ("error", "manageAdminFormChangeRootRole");
					TAdminView::getManageAdminForm($notification);
				}
				break;
			case ($countUsers > 1):
				$users = TModel::getUsersWithSearchWord($searchWord);
				$notification = new TAdminNotification ("success", "manageAdminFormFoundUsersSuccess");
				TAdminView::getManageAdminForm($notification, $users, $countUsers);
				
				break;
		}
	}
	else {				  
		// Ansprechen der View um das Formular abzurufen
		$notification = new TAdminNotification ("error", "manageAdminFormTextEmpy");
		TAdminView::getManageAdminForm($notification);
	}
}
else {
	if (isset($_POST['findAdmins'])) {
		$users = TModel::findAdminUsers();
		$notification = new TAdminNotification("success", "manageAdminFormFoundUsersSuccess");
		TAdminView::getManageAdminForm($notification, $users, count($users));	
	}
	else {
		if (isset($_POST['submitChoice'])) {
			if (isset($_POST['users'])) {
				$tuser = TUser::getUserById($_POST['users']);
				
				if ($tuser->getNickname() != 'root') {
					if ($_SESSION['user']->getUserid() == $tuser->getUserid()) {
						// Ansprechen der View um das Formular abzurufen
						$notification = new TAdminNotification ("error", "manageAdminFormChangeOwnRole");
						TAdminView::getManageAdminForm($notification);
					}
					else {
						$notification = new TAdminNotification ("", "");
						TAdminView::getManageUserRightsForm($notification, $tuser);
					}
				}
				else {
					// Ansprechen der View um das Formular abzurufen
					$notification = new TAdminNotification ("error", "manageAdminFormChangeRootRole");
					TAdminView::getManageAdminForm($notification);
				}
					
			}
			else {
				// Ansprechen der View um das Formular abzurufen
				$notification = new TAdminNotification ("error", "manageAdminFormNoSelection");
				TAdminView::getManageAdminForm($notification);
			}
		}
		else {
			if (isset($_POST['changeRights'])) {
				$user = TUser::getUserById($_POST['ID']);
				$user->setRole($_POST['rights']);
				
				$notification = new TAdminNotification ("success", "manageAdminFormSuccess");
				TAdminView::getManageAdminForm($notification);
			}
			else {
				// Ansprechen der View um das Formular abzurufen
				$notification = new TAdminNotification ("", "");
				TAdminView::getManageAdminForm($notification);
			}
		}
	}
}

include ("inc/footer.php");
?>
